package com.ruoyi.store.service.impl;

import com.ruoyi.major.mapper.StuMagorMapper;
import com.ruoyi.store.domain.*;
import com.ruoyi.store.mapper.StuExamDetailMapper;
import com.ruoyi.store.mapper.StuPaperMapper;
import com.ruoyi.store.mapper.StuQuestionMapper;
import com.ruoyi.store.service.IMemberService;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class MemberImpl implements IMemberService {

    @Resource
    private StuQuestionMapper stuQuestionMapper;
    @Resource
    private StuExamDetailMapper stuExamDetailMapper;
    @Resource
    private StuPaperMapper stuPaperMapper;
    @Resource
    private SysUserRoleMapper userRoleMapper;
    @Resource
    private StuMagorMapper stuMagorMapper;

    @Override
    public QuesCounts selectQuesCounts(QuesCounts quesCounts) {
        Long singleCounts = stuQuestionMapper.selectSingleCounts();
        Long manyCounts = stuQuestionMapper.selectManyCounts();
        Long judgeCounts = stuQuestionMapper.selectJudgeCounts();
        Long answerCounts = stuQuestionMapper.selectAnswerCounts();
        QuesCounts quesCounts1 = new QuesCounts();
        quesCounts1.setSingleCount(singleCounts);
        quesCounts1.setManyCount(manyCounts);
        quesCounts1.setJudgeCount(judgeCounts);
        quesCounts1.setAnswerCount(answerCounts);
        return quesCounts1;
    }

    @Override
    public HardCounts selectHardCounts(HardCounts hardCounts) {
        HardCounts hardCounts1 = new HardCounts();
        hardCounts1.setSimpleCount(stuQuestionMapper.selectSimpleCounts());
        hardCounts1.setCommonCount(stuQuestionMapper.selectCommonCounts());
        hardCounts1.setHardCount(stuQuestionMapper.selectHardCounts());


        return hardCounts1;
    }

    @Override
    public PaperCounts selectPaperCounts(Long paperId) {
        List<StuExamDetail> stuExamDetails = stuExamDetailMapper.selectStuExamByPaperId(paperId);
        StuPaper stuPaper = stuPaperMapper.selectStuPaperByPaperId(paperId);
        int passMark = stuPaper.getPassMark();
        long passCounts = 0L;
        long failCounts = 0L;
        long fineCounts = 0L;
        for (StuExamDetail stuExamDetail:stuExamDetails){
            Long totalScore = stuExamDetail.getTotalScore();
            if (totalScore>passMark){
                passCounts += 1;
                if (totalScore>passMark*0.8){
                    fineCounts += 1;
                }
            }else if (totalScore < passMark){
                failCounts += 1;
            }
        }

        PaperCounts paperCounts = new PaperCounts();
        paperCounts.setFailCounts(failCounts);
        paperCounts.setFineCounts(fineCounts);
        paperCounts.setPassCounts(passCounts);

        return paperCounts;
    }

    @Override
    public IndexCounts selectIndexCounts() {
        IndexCounts indexCounts = new IndexCounts();

        Long userCounts = userRoleMapper.countUserRoleByUser();
        Long quesCounts = stuQuestionMapper.countQues();
        Long paperCounts = stuPaperMapper.countPaper();
        Long magorCounts = stuMagorMapper.countMagor();

        indexCounts.setPeopleCounts(userCounts);
        indexCounts.setPaperCounts(paperCounts);
        indexCounts.setQuesCounts(quesCounts);
        indexCounts.setMagorCounts(magorCounts);


        return indexCounts;
    }
}
